package uranium.gui.view;

import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.JLabel;
import java.awt.Rectangle;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Date;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;

import uranium.db.ConnectionManager;
import uranium.db.DBManager;
import uranium.db.model.TableModelGenerics;
import javax.swing.JTextField;

import org.jdesktop.swingx.JXTable;

public class ViewSospesiGUI extends JDialog{

	private JFrame jFrame = null;  //  @jve:decl-index=0:visual-constraint="42,26"
	private JPanel jContentPane = null;
	private JButton jButton = null;
	private JButton jButton1 = null;
	private JScrollPane jScrollPane = null;
	private JXTable jTable1 = null;
	private DBManager dbm = null;
	private JLabel jLabel = null;
	private JTextField jTextField = null;
	private TableModelGenerics model = null;
	private JButton jButton2 = null;
	private Connection connessione;
	private Statement st = null;
	private ResultSet rs = null;  //  @jve:decl-index=0:
	private ConnectionManager cm;
	
	public ViewSospesiGUI(DBManager dbm, JFrame jFrame, ConnectionManager cm){
		super(jFrame, true);
		this.jFrame = jFrame;
		this.dbm = dbm;
		this.cm = cm;
		this.connessione = cm.getConnection();
		try{
			st = connessione.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
		} catch (SQLException e39){

			e39.printStackTrace();
		}
		initialize();
	}
	
	private void initialize(){
		//jFrame = new JFrame();
		this.setSize(new Dimension(639, 468));
		this.setTitle("Visualizza Sospesi");
		this.setContentPane(getJContentPane());
		centro();
		this.setVisible(true);
	}
	
	private void centro()
	{
		int x = (int) (Toolkit.getDefaultToolkit().getScreenSize().getWidth() - this.getWidth()) / 2;
		int y = (int) (Toolkit.getDefaultToolkit().getScreenSize().getHeight() - this.getHeight()) / 2;
		this.setLocation(x, y);
	}
	
	/*class MyMouseAdapter extends MouseAdapter{

		public void mouseClicked(MouseEvent e){

			System.out.println("Numero click : " + e.getClickCount());
			if (e.getButton() == MouseEvent.BUTTON1 && e.getClickCount() == 2 && e.getSource() == jTable1)
			{
				// Selezioniamo la riga e prendiamo il
				// suo ID che rappresenta la riga nel DB
				JTable table = (JTable) e.getSource();
				int row = table.getSelectedRow();
				if (row < 0) return;
				String codice = String.valueOf(table.getValueAt(row, 0));
				int scelta = JOptionPane.showConfirmDialog(jFrame, "Sei sicuro?", "Conferma modifica stato pagamento", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
				if (scelta==0){
					modificaStato(codice);
				}
			}

		}

	}*/
	
	/**
	 * This method initializes jContentPane	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jLabel = new JLabel();
			jLabel.setBounds(new Rectangle(130, 50, 130, 16));
			jLabel.setText("Ricerca per Cliente");
			jContentPane = new JPanel();
			jContentPane.setLayout(null);
			jContentPane.add(getJButton(), null);
			jContentPane.add(getJButton1(), null);
			jContentPane.add(getJScrollPane(), null);
			jContentPane.add(jLabel, null);
			jContentPane.add(getJTextField(), null);
			jContentPane.add(getJButton2(), null);
		}
		return jContentPane;
	}

	/**
	 * This method initializes jButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton() {
		if (jButton == null) {
			jButton = new JButton();
			jButton.setBounds(new Rectangle(320, 60, 90, 30));
			jButton.setText("Cerca");
			jButton.addMouseListener(new java.awt.event.MouseAdapter() {
				public void mouseClicked(java.awt.event.MouseEvent e) {
					try{
						model = new TableModelGenerics(dbm,"cliente",jTextField.getText(),"sospesi");
						jTable1.setModel(model);						
					}
					catch(Exception e29){
						e29.printStackTrace();
					}
				}
			});
		}
		return jButton;
	}

	/**
	 * This method initializes jButton1	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton1() {
		if (jButton1 == null) {
			jButton1 = new JButton();
			jButton1.setBounds(new Rectangle(370, 390, 90, 30));
			jButton1.setText("OK");
			jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
				public void mouseClicked(java.awt.event.MouseEvent e) {
					try{
						close();
					}
					catch(Exception e28){
						System.err.println(e28);
					}
				}
			});
		}
		return jButton1;
	}

	/**
	 * This method initializes jScrollPane	
	 * 	
	 * @return javax.swing.JScrollPane	
	 */
	private JScrollPane getJScrollPane() {
		if (jScrollPane == null) {
			jScrollPane = new JScrollPane();
			jScrollPane.setBounds(new Rectangle(15, 120, 601, 241));
			jScrollPane.setViewportView(getJTable1());
		}
		return jScrollPane;
	}

	/**
	 * This method initializes jTable1	
	 * 	
	 * @return javax.swing.JTable	
	 */
	private JTable getJTable1() {
		if (jTable1 == null) {
			jTable1 = new JXTable();
			model = new TableModelGenerics(dbm, "", "", "sospesi");
			jTable1.setModel(model);
			jTable1.setRowSelectionAllowed(true);
			jTable1.getCellEditor();
			jTable1.setColumnControlVisible(true);
			jTable1.addMouseListener(new java.awt.event.MouseAdapter() {
				public void mouseClicked(java.awt.event.MouseEvent e) {
					System.out.println("Numero click : " + e.getClickCount());
					if (e.getButton() == MouseEvent.BUTTON1 && e.getClickCount() == 1 && e.getSource() == jTable1){
						int row = jTable1.getSelectedRow();
						int col = jTable1.getSelectedColumn();
						if( col == 6 ){
							String c = (String)jTable1.getValueAt(row, col);
							int index = c.lastIndexOf("-");
							String codice = c.substring(index+1);
							displayCliente(codice);
						}
						
							
					}
				}
			});
		}
		return jTable1;
	}
	
	private void displayCliente(final String codice){
		//final ViewVenditeGUI padre = this;
		//final ViewVenditeGUI padre = this;
		SwingUtilities.invokeLater(new Runnable(){
			public void run(){
				ViewInfoClienteGUI cerca = new ViewInfoClienteGUI(dbm, jFrame, codice);
				//cerca.setVisible(true);
			}
		});
	}

	/**
	 * This method initializes jTextField	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextField() {
		if (jTextField == null) {
			jTextField = new JTextField();
			jTextField.setBounds(new Rectangle(130, 71, 109, 20));
		}
		return jTextField;
	}

	/**
	 * This method initializes jButton2	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton2() {
		if (jButton2 == null) {
			jButton2 = new JButton();
			jButton2.setBounds(new Rectangle(160, 390, 90, 30));
			jButton2.setText("Aggiorna");
			jButton2.addMouseListener(new java.awt.event.MouseAdapter() {
				public void mouseClicked(java.awt.event.MouseEvent e) {
					System.out.println("mouseClicked()"); // TODO Auto-generated Event stub mouseClicked()
					modificaStato();
					model = new TableModelGenerics(dbm, "", "", "sospesi");
					jTable1.setModel(model);
				}
			});
		}
		return jButton2;
	}
	
	/*private synchronized void aggiorna(){
		String query = "SELECT * FROM sospesi";
		try{
			rs = st.executeQuery(query);
			if (rs.next()){
				String p = (String)rs.getString("pagato");
				if (p.equalsIgnoreCase("SI")){
					String codice = (String)rs.getString("codice");
					String modello = (String)rs.getString("descrizione");
					String cliente = (String)rs.getString("cliente");
					
					int qta = (Integer)rs.getInt("qta");
					double prezzoin = (double)rs.getDouble("prezzoin");
					double prezzout = (double)rs.getDouble("prezzoout");
					Date data = rs.getDate("data");
					String delete = "DELETE FROM sospesi WHERE codice='"+codice+"'";
					String insert = "INSERT INTO vendite (codice,descrizione,prezzoin,prezzoout,data,qta,cliente) VALUES('"+codice+"','"+modello+"','"+prezzoin+"','"+prezzout+"','"+data+"','"+qta+"','"+cliente+"')";
					dbm.executeQuery(insert);
					dbm.executeQuery(delete);
				}
			}
		}catch (Exception e1){
			e1.printStackTrace();
		}
	}*/
	
	private void close(){
		this.dispose();
	}
	
	private synchronized void modificaStato(){
		int row = jTable1.getSelectedRow();
		if (row < 0) 
			JOptionPane.showMessageDialog(jFrame, "Selezionare una riga", "AVVISO", JOptionPane.INFORMATION_MESSAGE);
		else{
			int scelta = JOptionPane.showConfirmDialog(jFrame, "Sei sicuro?", "Conferma modifica stato pagamento", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
			if (scelta==0){
				String codice = ((String)jTable1.getValueAt(row, 0));
				String query = "SELECT * FROM sospesi WHERE codice='"+codice+"'";
				try{
					rs = st.executeQuery(query);
					if (rs.next()){
						String modello = (String)rs.getString("descrizione");
						String cliente = (String)rs.getString("cliente");
						int qta = (Integer)rs.getInt("qta");
						double prezzoin = (double)rs.getDouble("prezzoin");
						double prezzout = (double)rs.getDouble("prezzoout");
						double guadagno = (double)rs.getDouble("guadagno");
						String data = rs.getString("data");
						String delete = "DELETE FROM sospesi WHERE codice='"+codice+"'";
						String insert = "INSERT INTO vendite (codice,descrizione,prezzoin,prezzoout,guadagno,data,qta,cliente) VALUES('"+codice+"','"+modello+"','"+prezzoin+"','"+prezzout+"','"+guadagno+"','"+data+"','"+qta+"','"+cliente+"')";
						dbm.executeQuery(insert);
						dbm.executeQuery(delete);
					}
				}
				catch (Exception e1){
					e1.printStackTrace();
				}
			}
		}
	}
}
